package com.smasher.location.proxy;

import android.content.Context;
import android.location.LocationManager;
import android.text.TextUtils;

import com.smasher.location.actual.LocationAMap;
import com.smasher.location.actual.LocationNative;
import com.smasher.location.eums.LocationMode;
import com.smasher.location.listener.LocationCallback;
import com.smasher.location.listener.onLocationListener;

public class LocationProxy {

    private LocationNa locationImp;
    private String locationDescription;
    private onLocationListener onLocationListener;
    private LocationManager locationManager;

    public void setMode(LocationMode mode) {
        switch (mode) {
            case NATIVE:
                setLocationNa(new LocationNative());
                break;
            case AMAP:
                setLocationNa(new LocationAMap());
                break;
            default:
                setLocationNa(new LocationAMap());
                break;
        }
    }

    private void setLocationNa(LocationNa locationNa) {
        locationImp = locationNa;
    }

    public void init(Context context) {
        locationManager =
                (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (locationImp != null) {
            locationImp.init(context);
            locationImp.setCallback(locationCallback);
        }

    }

    public void start() {
        if (!locationEnable()) {
            locationCallback.onFailed("定位服务未开启");
            return;
        }
        if (locationImp != null) {
            locationImp.start();
        }
    }

    public void stop() {
        if (locationImp != null) {
            locationImp.stop();
        }
    }

    public void release() {
        if (locationImp != null) {
            locationImp.release();
        }
    }

    public void setLocationListener(onLocationListener onLocationListener) {
        this.onLocationListener = onLocationListener;
    }

    private final LocationCallback locationCallback = new LocationCallback() {
        @Override
        public void onSuccess(String location, String address) {
            if (!TextUtils.isEmpty(address)) {
                locationDescription = address;
                /// updateLocationAddress(address);
                /// Toasty.success(CustomerFollowup.this, address).show();
                if (onLocationListener != null) {
                    onLocationListener.onLocation(address);
                }
            } else {
                if (onLocationListener != null) {
                    onLocationListener.onError("address is empty");
                }
            }

        }

        @Override
        public void onFailed(String reason) {
            if (onLocationListener != null) {
                onLocationListener.onError(reason);
            }
        }

        @Override
        public void onCompleted() {
            if (onLocationListener != null) {
                onLocationListener.onComplete();
            }
        }
    };

    public String getLocationDescription() {
        return locationDescription;
    }

    public void setLocationDescription(String locationDescription) {
        this.locationDescription = locationDescription;
    }

    private boolean locationEnable() {
        try {
            boolean gpsEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            boolean networkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            return gpsEnable || networkEnable;
        } catch (Exception e) {
            return true;
        }
    }

}
